package cn.mall.product.web;


import cn.mall.common.vo.MemberEntity;
import cn.mall.product.entity.CategoryEntity;

import cn.mall.product.feign.OpenFeignMemberService;
import cn.mall.product.service.CategoryService;
import cn.mall.product.vo.Catalog2Vo;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

import static cn.mall.common.constant.CartConstant.USER_PREFIX;

@Controller
public class IndexController {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private OpenFeignMemberService controller;
    private RedisTemplate redisTemplate;

    @GetMapping({"/","/index.html"})
    public  String indexPage(@RequestParam(value = "id",required = false) String id, Model model, ModelMap map, HttpServletRequest request){
        if (id!=null){
            long memberId = Long.parseLong(id.substring(0, id.indexOf(".")));
            System.out.println("id!=null 方法执行中" + memberId);
            Map<String, MemberEntity> memberEntitydata = (Map<String, MemberEntity>) controller.info(memberId).get("member");
            map.put("nickname",memberEntitydata.get("nickname"));
            map.put("header",memberEntitydata.get("header"));
            System.out.println("用户信息-------------------------------"+memberEntitydata);
            model.addAttribute("MemberEntityS",memberEntitydata);
        }else {
            Cookie[] cookies = request.getCookies();
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                  if ("userId".equals(cookie.getName())){
                      Long userId= Long.valueOf(cookie.getValue());
                      System.out.println("userId =111111111111111111 " + userId);
                      Map<String,MemberEntity> memberEntitydata = (Map<String, MemberEntity>) controller.info(userId).get("member");
                      System.out.println(" cookie中的数据===================== = " + memberEntitydata);
                      map.put("nickname",memberEntitydata.get("nickname"));
                      map.put("header",memberEntitydata.get("header"));
//                      Object o = redisTemplate.opsForHash().get(JSON.toJSONString(USER_PREFIX), JSON.toJSONString(USER_PREFIX+userId));
//                      BoundHashOperations boundHashOperations = redisTemplate.boundHashOps(USER_PREFIX);
//                      System.out.println("ooooooo = " + boundHashOperations.randomEntry());
                  }
                }
            } else {
                System.out.println("No cookies found.");
            }
        }
        //1.查出1级分类
        List<CategoryEntity> categoryEntities = categoryService.getLevel1Category();
        model.addAttribute("categorys",categoryEntities);
        return "index";
    }


    @ResponseBody
    @GetMapping("/index/catalog.json")
    public  Map<String,List<Catalog2Vo>> getCatelogJosn(){

        Map<String,List<Catalog2Vo>> catalogJson= categoryService.getCatalogJson();
        return catalogJson;
    }


}
